﻿using System.IO.Compression;
using Wips.Infrastructure.Model;

namespace Wips.Infrastructure.Helpers
{
    public static class ZipHelper
    {
        /// <summary>
        /// 获取压缩文件里面的内容
        /// </summary>
        /// <param name="zipFilePath"></param>
        /// <returns></returns>
        public static List<DirectoryModel> ListZipContents(string zipFilePath)
        {
            var result = new List<DirectoryModel>();
            using (var zip = ZipFile.OpenRead(zipFilePath))
            {
                foreach (var entry in zip.Entries)
                {
                    if (entry.FullName.EndsWith("/"))
                    {

                        result.Add(new DirectoryModel { CanRead = true, CanWrite = true, Path = entry.FullName, Type = EnumDirectorType.Directory });

                    }
                    else
                    {
                        result.Add(new DirectoryModel { CanRead = true, CanWrite = true, Path = entry.FullName, Type = EnumDirectorType.File });
                    }
                }
            }
            return result;
        }
        public static void CreateCustomZip(string rootPath, string backPath, string zipFileName, List<string> includedPaths)
        {
            var fullPath = Path.Combine(backPath, zipFileName);
            // 创建一个临时目录
            var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            Directory.CreateDirectory(tempDirectory);
            if (!Directory.Exists(backPath))
            {
                Directory.CreateDirectory(backPath);
            }

            try
            {
                // 将需要的文件和目录复制到临时目录
                foreach (var path in includedPaths)
                {
                    var relativePath = Path.GetRelativePath(rootPath, path);
                    var destinationPath = Path.Combine(tempDirectory, relativePath);

                    if (Directory.Exists(path))
                    {
                        // 这是一个目录，复制整个目录
                        Directory.CreateDirectory(destinationPath);
                        foreach (var file in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
                        {
                            var relativeFilePath = Path.GetRelativePath(path, file);
                            var destFile = Path.Combine(destinationPath, relativeFilePath);
                            Directory.CreateDirectory(Path.GetDirectoryName(destFile)!);
                            File.Copy(file, destFile, true);
                        }
                    }
                    else if (File.Exists(path))
                    {
                        // 这是一个文件，复制文件
                        Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!);
                        File.Copy(path, destinationPath, true);
                    }
                }

                // 创建压缩文件
                if (File.Exists(fullPath))
                {
                    File.Delete(fullPath);
                }
                ZipFile.CreateFromDirectory(tempDirectory, fullPath);
            }
            finally
            {
                // 删除临时目录
                Directory.Delete(tempDirectory, true);
            }
        }


        /// <summary>
        /// 解压覆盖
        /// </summary>
        /// <param name="zipFilePath"></param>
        /// <param name="destinationDirectory"></param>
        public static void ExtractAndOverwriteZipFile(string zipFilePath, string destinationDirectory)
        {
            using (var archive = ZipFile.OpenRead(zipFilePath))
            {
                foreach (var entry in archive.Entries)
                {
                    var destinationPath = Path.Combine(destinationDirectory, entry.FullName);

             
                    // 创建目标路径的目录
                    var destinationDir = Path.GetDirectoryName(destinationPath);
                    if (!Directory.Exists(destinationDir))
                    {
                        Directory.CreateDirectory(destinationDir!);
                    }
                    // 解压缩并覆盖文件
                    if (!entry.FullName.EndsWith("/")) // 避免尝试解压目录
                    {
                        entry.ExtractToFile(destinationPath, overwrite: true);
                    }
                }
            }
        }
    }
}
